package com.pyue.eam.admin.api.except;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.pyue.eam.common.excel.vo.ErrorMessage;
import lombok.extern.java.Log;
import org.springframework.validation.BindingResult;

import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * @author ludashi
 * @since 2021-10-28
 */
@Log
public class ApiAsserts {

    protected ApiAsserts() {
        // to do noting
    }

    /**
     * 断言true
     */
    public static void isTrue(Boolean obj, String message) {
        fail(!obj, message);
    }

    /**
     * 断言true
     */
    public static void isTrue(Boolean bool1, String message1, Boolean bool2, String message2) {
        isTrue(bool1, message1, bool2, message2, Boolean.TRUE, StrUtil.EMPTY);
    }

    /**
     * 断言true
     */
    public static void isTrue(Boolean bool1, String message1, Boolean bool2, String message2, Boolean bool3, String message3) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(StrUtil.C_BRACKET_START);
        if (!bool1) {
            stringBuilder.append(message1);
            stringBuilder.append(StrUtil.COMMA);
        }
        if (!bool2) {
            stringBuilder.append(message2);
            stringBuilder.append(StrUtil.COMMA);
        }
        if (!bool3) {
            stringBuilder.append(message3);
            stringBuilder.append(StrUtil.COMMA);
        }
        stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length(), StrUtil.EMPTY);
        stringBuilder.append(StrUtil.C_BRACKET_END);
        fail(!bool1 || !bool2 || !bool3, stringBuilder.toString());
    }

    /**
     * 断言false
     */
    public static void isFalse(Boolean obj, String message) {
        fail(obj, message);
    }


    /**
     * 断言导入数据 不为空
     *
     * @param obj    导入数据
     * @param result 错误信息
     */
    public static void checkImportData(List obj, BindingResult result) {
        isTrue(CollUtil.isNotEmpty(obj) && CollUtil.isEmpty((List<ErrorMessage>) result.getTarget()), "导入失败，请检查数据");
        isTrue(CollUtil.size(obj) <= 2000, "导入失败，导入数据不能超过2000条");
    }

    /**
     * 断言不空
     */
    public static void isEmpty(Object obj, String message) {
        fail(!ObjectUtils.isEmpty(obj), message);
    }

    /**
     * 断言不为空
     */
    public static void nonEmpty(Object obj, String message) {
        fail(ObjectUtils.isEmpty(obj), message);
    }

    /**
     * 断言相同
     */
    public static void equals(Object a, Object b, String message) {
        fail(!Objects.equals(a, b), message);
    }

    /**
     * 断言不相同
     */
    public static void nonEquals(Object a, Object b, String message) {
        fail(Objects.equals(a, b), message);
    }

    /**
     * 断言包括
     */
    public static void contains(List list, Object object, String message) {
        fail(!list.contains(object), message);
    }


    public static void fail(String message) {
        throw new ApiException(message);
    }

    private static void fail(boolean condition, Supplier<String> supplier) {
        fail(condition, supplier.get());
    }

    private static void fail(boolean condition, String message) {
        if (condition) {
            log.severe(message);
            fail(message);
        }
    }
}
